package com.example.springbootcourse.service.impl;

import com.example.springbootcourse.mapper.PowerMapper;
import com.example.springbootcourse.mapper.RoleMapper;
import com.example.springbootcourse.pojo.Power;
import com.example.springbootcourse.pojo.PowerExample;
import com.example.springbootcourse.pojo.Role;
import com.example.springbootcourse.req.RoleReq;
import com.example.springbootcourse.service.RoleService;
import com.example.springbootcourse.util.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {


    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PowerMapper powerMapper;

    @Override
    public List<RoleReq> queryRole() {

        List<RoleReq> roleReqs = roleMapper.queryRoleAndPower();

        return roleReqs;
    }

    @Override
    public void editRole(RoleReq roleReq) {
        Role role = CopyUtil.copy(roleReq, Role.class);

        Power power = new Power();
        power.setPower(roleReq.getPower());
        roleMapper.updateByPrimaryKey(role);
        PowerExample example = new PowerExample();
        PowerExample.Criteria criteria = example.createCriteria();
        criteria.andRoleidEqualTo(roleReq.getPowerid());
        powerMapper.updateByExampleSelective(power,example);

    }
}
